package com.tjetc.view.manage;

import com.tjetc.domain.Disease;
import com.tjetc.domain.Examination;
import com.tjetc.domain.Health;
import com.tjetc.domain.Newborn;
import com.tjetc.proxy.ClientProxy;
import com.tjetc.service.DiseaseService;
import com.tjetc.service.ExaminationService;
import com.tjetc.service.HealthService;
import com.tjetc.service.NewBornService;
import com.tjetc.vo.Result;

import java.util.List;
import java.util.Scanner;

public class DiseaseView {//
    Scanner sc = new Scanner(System.in);
    private DiseaseService diseaseService= ClientProxy.getProxyInstance(DiseaseService.class);
    private NewBornService newBornService=ClientProxy.getProxyInstance(NewBornService.class);
    private ExaminationService examinationService=ClientProxy.getProxyInstance(ExaminationService.class);
    private HealthService healthService=ClientProxy.getProxyInstance(HealthService.class);
    public void start(){
        while (true){
            try {
                System.out.println("1.对疾病进行增加");
                System.out.println("2.对疾病进行删除");
                System.out.println("3.对疾病进行修改");
                System.out.println("4.对疾病信息进行查询");
                System.out.println("5.查询某个疾病的患病人数");
                System.out.println("6.对新生儿进行疾病的检查");
                System.out.println("7.对新生儿检查的报告进行修改");
                System.out.println("8.对新生儿的检查报告进行分析,将疾病放入healthy表中");
                System.out.println("0.返回上一级");
                int i=sc.nextInt();
                switch (i){
                    case 1:
                        addDisease();
                        break;
                    case 2:
                        deleteDisease();
                        break;
                    case 3:
                        updateDisease();
                        break;
                    case 4:
                        findDisease();
                        break;
                    case 5:
                        findNumberOfDisease();
                        break;
                    case 6:
                        physicalExamination();//体检
                        break;
                    case 7:
                        updateExamination();
                        break;
                    case 8:
                        analysis();//分析
                        break;
                    case 0:
                        return;
                }
            } catch (Exception e) {
                System.out.println("输入错误");
                sc.nextLine();
            }
        }

    }

    private void analysis() {
        System.out.println("请输入你想要分析疾病的新生儿的id");
        int id = sc.nextInt();
        Examination examination = examinationService.findExamination(id);//获取体检信息
        if (examination==null){
            System.out.println("该新生儿没有体检");
            return;
        }else {
            List<Disease> all = diseaseService.findAll();//获取所有疾病
            for (Disease disease : all) {
                String flag= disease.getFlag().toString();
                int  threshold= disease.getThreshold();//获取阈值
                String attributeName= disease.getAttributeName();//属性名称
                if (flag.equals("大于")){
                    double elementByName = examination.getElementByName(attributeName);//根据元素名字获取元素值
                    if (elementByName>threshold){
                        Result result = healthService.addHealth(new Health(id, disease.getId()));
                        System.out.println( result.getMsg());
                    }
                }
                if (flag.equals("小于")){
                    double elementByName = examination.getElementByName(attributeName);
                    if (elementByName<threshold){
                        Result result = healthService.addHealth(new Health(id, disease.getId()));
                        if (result.getCode()==200){
                            System.out.println("添加成功");
                        }
                        if (result.getCode()==500){
                            System.out.println("添加失败");
                        }
                    }
                }
            }
        }


    }

    private void updateExamination() {
        System.out.println( "请输入需要更改的新生儿的编号");
        int id=sc.nextInt();
        Newborn bId = newBornService.findBId(id);
        if (bId==null){
            System.out.println("该新生儿不存在");
            return;
        }
        System.out.println("请输入新生儿的白细胞含量");
        double wbc=sc.nextDouble();
        System.out.println("请输入新生儿的红细胞数目");
        double rbc=sc.nextDouble();
        Double birthWeight = bId.getBirthWeight();
        Double birthHeight = bId.getBirthHeight();
        Examination examination = new Examination(id,birthHeight,birthWeight,wbc,rbc);
        Result result = examinationService.updateExamination(examination);
        System.out.println(result.getMsg());
    }

    private void physicalExamination() {
        System.out.println("输入要检查的新生儿编号");
        int id=sc.nextInt();
        Newborn bId = newBornService.findBId(id);
        if (bId==null){
            System.out.println("该新生儿不存在");
            return;
        }
        else {
            System.out.println("请输入新生儿的白细胞含量");
            double wbc=sc.nextDouble();
            System.out.println("请输入新生儿的红细胞数目");
            double rbc=sc.nextDouble();
            Double birthWeight = bId.getBirthWeight();
            Double birthHeight = bId.getBirthHeight();
            Examination examination = new Examination(id,birthHeight,birthWeight,wbc,rbc);//将检测成绩写到表中
            Result result = examinationService.addExamination(examination);
            System.out.println(result.getMsg());
        }
    }

    private void findNumberOfDisease() {
        System.out.println("请输入你想要查询数量的疾病id");
        int id = sc.nextInt();
        int numberOfDisease = diseaseService.findNumberOfDisease(id);
        System.out.println(numberOfDisease);


    }

    private void addDisease() {
        System.out.println("输入疾病的名称");
        String name=sc.next();
        System.out.println("输入疾病症状");
        String symptom=sc.next();
        System.out.println("输入判断疾病的元素");
        String attributeName=sc.next();
        System.out.println("输入疾病的判断方式（大于或小于）");
        String flag= sc.next().toString();
        System.out.println("输入疾病的阈值");
        int threshold=sc.nextInt();
        Disease disease = new Disease();
        disease.setName(name);
        disease.setSymptom(symptom);
        disease.setAttributeName(attributeName);
        disease.setFlag(flag);
        disease.setThreshold(threshold);
        diseaseService.addDisease(disease);
    }
    private void deleteDisease() {
        System.out.println("输入要删除的疾病编号");
        int id=sc.nextInt();
        diseaseService.deleteDisease(id);
    }
    private void updateDisease() {
        diseaseService.findAll();
        System.out.println("请输入需要修改的疾病的编号");
        int id=sc.nextInt();
        System.out.println("输入疾病的名称");
        String name=sc.next();
        System.out.println("输入疾病症状");
        String symptom=sc.next();
        System.out.println("输入判断疾病的元素");
        String attributeName=sc.next();
        System.out.println("输入疾病的判断方式（大于或小于）");
        String flag= sc.next().toString();
        System.out.println("输入疾病的阈值");
        int threshold=sc.nextInt();
        Disease disease = new Disease();
        disease.setId(id);
        disease.setName(name);
        disease.setSymptom(symptom);
        disease.setAttributeName(attributeName);
        disease.setFlag(flag);
        disease.setThreshold(threshold);
        diseaseService.updateDisease(disease);
    }
    private void findDisease() {
        System.out.println("输入要查询的疾病编号");
        int id=sc.nextInt();
        Disease disease = diseaseService.findDisease(id);
        System.out.println(disease);
    }
}
